﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 水牛管理系统.UI {
    public partial class Statistic : Form {
        public Statistic() {
            InitializeComponent();
            loadtab1();
            loadtab2();
            loadtab3();
        }

        private void Statistic_Load(object sender, EventArgs e) {
            {
                MySqlCommand cmd = new MySqlCommand("select sum(cost) as sum from log", new Utils.DatebaseUtil().open());
                MySqlDataReader reader = cmd.ExecuteReader();
                if (reader.HasRows) {
                    reader.Read();
                    label_income_total.Text = reader.GetString("sum") + "元";
                }
                else {
                    reader.Close();
                }
            }
            {
                MySqlCommand cmd = new MySqlCommand("select sum(cost) as sum from log left join user on user.user_card_id=log.user_card_id where date_format(log_date, '%Y-%m-%d')=date_format(DATE(NOW()), '%Y-%m-%d')", new Utils.DatebaseUtil().open());
                MySqlDataReader reader = cmd.ExecuteReader();
                if (reader.HasRows) {
                    reader.Read();
                    label_income_today.Text = reader.GetString("sum") + "元";
                }
                else {
                    reader.Close();
                }
            }
        }

        private void loadtab1() {
            try {
                DataSet ds = new DataSet();
                MySqlDataAdapter da = new MySqlDataAdapter("select user.user_name as 姓名,user.user_card_id as 卡号,sum(cost) as 消费金额 from log left join user on user.user_card_id=log.user_card_id where date_format(log_date, '%Y-%m-%d')=date_format(DATE(NOW()), '%Y-%m-%d') group by user.user_name", new Utils.DatebaseUtil().open());
                da.Fill(ds);
                dataGridView1.DataSource = ds.Tables[0];
            }
            catch (Exception e1) {
                MessageBox.Show(e1.Message);
            }
        }
        private void loadtab2() {
            try {
                DataSet ds = new DataSet();
                MySqlDataAdapter da = new MySqlDataAdapter("select user.user_name as 姓名,user.user_card_id as 卡号,sum(cost) as 消费金额 from log left join user on user.user_card_id=log.user_card_id where date_format(log_date, '%Y-%m')=date_format(DATE(NOW()), '%Y-%m') group by user.user_name", new Utils.DatebaseUtil().open());
                da.Fill(ds);
                dataGridView2.DataSource = ds.Tables[0];
            }
            catch (Exception e1) {
                MessageBox.Show(e1.Message);
            }
        }
        private void loadtab3() {
            try {
                DataSet ds = new DataSet();
                MySqlDataAdapter da = new MySqlDataAdapter("select user.user_name as 姓名,user.user_card_id as 卡号,sum(cost) as 消费金额 from log left join user on user.user_card_id=log.user_card_id where date_format(log_date, '%Y')=date_format(DATE(NOW()), '%Y') group by user.user_name", new Utils.DatebaseUtil().open());
                da.Fill(ds);
                dataGridView3.DataSource = ds.Tables[0];
            }
            catch (Exception e1) {
                MessageBox.Show(e1.Message);
            }
        }

        private void selectCount(object sender, EventArgs e) {
            int selectd = tabControl1.SelectedIndex;
            switch (selectd) {
                case 0:
                    break;
                case 1:
                    break;
                case 2:
                    break;
            }
        }
    }
}
